package com.tdk.wifidirectserver.nsd

import android.content.Context
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import android.util.Log

/**
 * NSD server端,负责发送广播
 * @property TAG String
 */
class NsdServer : INsdServer {
    private val TAG = "NsdServer"
    override fun registerService(
        context: Context,
        nsdServiceInfo: NsdServiceInfo,
        listener: NsdManager.RegistrationListener
    ) {
        var registerNsdManager: NsdManager =
            context.getSystemService(Context.NSD_SERVICE) as NsdManager
        try {
            Log.d(TAG, "registerService :$nsdServiceInfo}")
            registerNsdManager.registerService(nsdServiceInfo, NsdManager.PROTOCOL_DNS_SD, listener)
        } catch (e: Exception) {
            Log.e(TAG, "报错信息$TAG ${e.message}")
        }
    }

    override fun unregister(context: Context, listener: NsdManager.RegistrationListener) {
        var nsdManager = context.getSystemService(Context.NSD_SERVICE) as NsdManager
        try {
            nsdManager.unregisterService(listener)
        } catch (e: Exception) {
            Log.e(TAG, "NSD listener not registered")
        }
    }

}